/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan
 *          written for Code Plex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace UserGroupManager
{
    public static class ErrorLogger
    {
        public static void LogError(string msg, string stkTrace)
        {
            WriteToErrorLog(msg, stkTrace, "UserGroupManager");
            WriteToEventLog (msg, 100 );
        }

        // adapted and modified from: 
        // http://www.c-sharpcorner.com/UploadFile/scottlysle/EventAndErrorLogging12152006003023AM/EventAndErrorLogging.aspx
        private static void WriteToErrorLog(string msg, string stkTrace, string title)
        {
            try
            {
                string sFilename = "userMgrErr.log";
                if (!(System.IO.Directory.Exists(Environment.CurrentDirectory + "\\Errors\\")))
                {
                    System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + "\\Errors\\");
                }

                FileStream fs = new FileStream(Environment.CurrentDirectory +
                    "\\Errors\\" + sFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                StreamWriter s = new StreamWriter(fs);
                s.Close();
                fs.Close();

                FileStream fs1 = new FileStream(Environment.CurrentDirectory + "\\Errors\\" + sFilename,
                    FileMode.Append, FileAccess.Write);

                StreamWriter s1 = new StreamWriter(fs1);

                s1.Write("Title: \t\t" + title + "\r\n");
                s1.Write("Message: \t" + msg + "\r\n");
                s1.Write("StackTrace: \t" + stkTrace + "\r\n");
                string repChar = new String('=', 80);
                s1.Write("Date/Time: \t" + DateTime.Now.ToString() + "\r\n");
                s1.Write(repChar + "\r\n");

                // close streams
                s1.Close();
                fs1.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        // from msdn -
        // http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.writeentry.aspx
        private static void WriteToEventLog(string msg, int appEventId)
        {
            // Create the source, if it does not already exist.
            if (!EventLog.SourceExists("Application"))
            {
                // An event log source should not be created and immediately used.
                // There is a latency time to enable the source, it should be created
                // prior to executing the application that uses the source.
                // Execute this sample a second time to use the new source.
                EventLog.CreateEventSource("Application", "UserGroupManager");
                Console.WriteLine("Creating EventSource");
                Console.WriteLine("Exiting, execute the application a second time to use the source.");
                // The source is created.  Exit the application to allow it to be registered.
                return;
            }

            // Set the 'description' for the event.
            EventLogEntryType logEntryType = EventLogEntryType.Error;
            msg = msg + "\r\n\r\nPlease contact your admin";

            // Write the entry in the event log.
            // Console.WriteLine("Writing to EventLog.. ");
            EventLog.WriteEntry("UserGroupManager", msg, logEntryType, appEventId);

        }
    }
}
